//
//  YCOrderQueryViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/21.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCOrderQueryViewController.h"
#define YCOrderDealCellIdentify @"YCOrderDealTableViewCell"
#define YCOrderUnSendCellIdentify @"YCOrderUnSendTableViewCell"

@interface YCOrderQueryViewController ()
{
    UIScrollView* _contentscrollview;
    UITableView *_tableView1;
    UITableView *_tableView2;
    UITableView *_tableView3;
    YCEmptyView *_emptyView1;
    YCEmptyView *_emptyView2;
    YCEmptyView *_emptyView3;
    
    NSInteger _currentPage;
    NSMutableArray *_arrayList1;
    NSMutableArray *_arrayList2;
    NSMutableArray *_arrayList3;
    
    ScrollBar *_scrollBar;
    NSInteger _currentIndex;
    UIButton *m_rightBtn;
    ShopTable *_shopTable;
}
@end

@implementation YCOrderQueryViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:NO];
    [_naviVC setCommonNaviVC];
    _naviVC.alphaView.layer.shadowPath=nil;
    _naviVC.alphaView.layer.shadowOpacity = 0;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"历史订单";
    
    YCNavigationViewController *navigationController =(YCNavigationViewController *) self.navigationController;
    navigationController.alphaView.layer.shadowPath=nil;
    navigationController.alphaView.layer.shadowOpacity = 0;
    
    _arrayList1=[NSMutableArray array];
    _arrayList2=[NSMutableArray array];
    _arrayList3=[NSMutableArray array];
    [[App shared]readShopData];
    _shopTable=[App shared].shop;
    
    [self addMainView];
    [self loadData];
}
-(void)loadData
{
    OrderIndexRequest *request = [OrderIndexRequest new];
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    request.shop_id=_shopTable.Id;
    request.status=[NSString stringWithFormat:@"%ld",(long)_currentIndex+6];
    request.page=[NSString stringWithFormat:@"%ld",(long)_currentPage];
    [apiClient doOrderIndex:request success:^(ApiResponse *data, NSString *url) {
        OrderIndexResponse *response=[[OrderIndexResponse alloc]initWithCGResponse:data];
        
        _scrollBar.numArray=@[response.effective_count,response.refunded_count,response.canceled_count];
        [_scrollBar reloadHeaderData];
        
        [self reloadDataWith:response];
    }failure:^(ApiResponse *data, NSString *url){
        
    }];
}
-(void)reloadDataWith:(OrderIndexResponse *)response
{
    if (_currentIndex==0) {
        if (_currentPage>1) {
            [_arrayList1 addObjectsFromArray:response.list];
        }else{
            _arrayList1=[NSMutableArray arrayWithArray:response.list];
        }
        [_tableView1 reloadData];
        [_emptyView1 setHidden:!(_arrayList1.count==0)];
        
        if (response.list.count==0) {
            _tableView1.mj_footer.state=MJRefreshStateNoMoreData;
        }else{
            [_tableView1.mj_footer endRefreshing];
        }
    }
    if (_currentIndex==1) {
        if (_currentPage>1) {
            [_arrayList2 addObjectsFromArray:response.list];
        }else{
            _arrayList2=[NSMutableArray arrayWithArray:response.list];
        }
        [_tableView2 reloadData];
        [_emptyView2 setHidden:!(_arrayList2.count==0)];
        
        if (response.list.count==0) {
            _tableView2.mj_footer.state=MJRefreshStateNoMoreData;
        }else{
            [_tableView2.mj_footer endRefreshing];
        }
    }
    if (_currentIndex==2) {
        if (_currentPage>1) {
            [_arrayList3 addObjectsFromArray:response.list];
        }else{
            _arrayList3=[NSMutableArray arrayWithArray:response.list];
        }
        [_tableView3 reloadData];
        [_emptyView3 setHidden:!(_arrayList3.count==0)];
        
        if (response.list.count==0) {
            _tableView3.mj_footer.state=MJRefreshStateNoMoreData;
        }else{
            [_tableView3.mj_footer endRefreshing];
        }
    }
}
-(void)addMainView
{
    UIView *headerview=[[UIView alloc]initWithFrame:CGRectMake(0, kNAV_HEIGHT, kMainScreen_Width, 75)];
    [headerview setBackgroundColor:WHITE_COLOR];
    [self.view addSubview:headerview];
    
    NSArray *btnArray =@[@"有效订单",@"退款订单",@"取消订单"];
    _scrollBar = [[ScrollBar alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, H(headerview))];
    [_scrollBar setBackgroundColor:WHITE_COLOR];
    _scrollBar.btnArray = btnArray;
    _scrollBar.numArray=@[@"0",@"0",@"0"];
    [_scrollBar setHeaderData];
    _scrollBar.selectColor=MAIN_COLOR;
    _scrollBar.currentSelect=_currentIndex;
    _scrollBar.delegate = self;
    [headerview addSubview:_scrollBar];
    
    
    CGFloat height=kMainScreen_Height-H(headerview)-kNAV_HEIGHT-kTAB_HEIGHT;
    _contentscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNAV_HEIGHT+H(headerview), kMainScreen_Width, height)];
    [self.view addSubview:_contentscrollview];
    _contentscrollview.delegate = self;
    [_contentscrollview setContentSize:CGSizeMake(kMainScreen_Width*3, height)];
    _contentscrollview.pagingEnabled = YES;
    _contentscrollview.bounces = NO;
    _contentscrollview.showsHorizontalScrollIndicator=NO;
    
    
    _currentPage=1;
    _tableView1 =[self addTableView:0];
    _tableView2 =[self addTableView:1];
    _tableView3 =[self addTableView:2];
    
    _arrayList1=[NSMutableArray array];
    _arrayList2=[NSMutableArray array];
    _arrayList3=[NSMutableArray array];
    
    m_rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    [m_rightBtn setImage:[UIImage imageNamed:@"order_search"] forState:UIControlStateNormal];
    [m_rightBtn addTarget:self action:@selector(btnSearchClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:m_rightBtn];
}
-(UITableView*)addTableView:(NSInteger)page{
    CGFloat height=kMainScreen_Height-75-kNAV_HEIGHT-kTAB_HEIGHT;
    UITableView* tableview=[[UITableView alloc]initWithFrame:CGRectMake(page*W(_contentscrollview),0, kMainScreen_Width,height )];
    [tableview setBackgroundColor:BACK_COLOR];
    tableview.delegate=self;
    tableview.dataSource=self;
    tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    [tableview registerNib:[UINib nibWithNibName:YCOrderDealCellIdentify bundle:nil] forCellReuseIdentifier:YCOrderDealCellIdentify];
    [tableview registerNib:[UINib nibWithNibName:YCOrderUnSendCellIdentify bundle:nil] forCellReuseIdentifier:YCOrderUnSendCellIdentify];
    
    tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage=1;
        [self loadData];
        [tableview.mj_header endRefreshing];
    }];
    tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _currentPage++;
        [self loadData];
    }];

    [_contentscrollview addSubview:tableview];
    return tableview;
}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==_tableView1) {
        return _arrayList1.count;
    }
    if (tableView==_tableView2) {
        return _arrayList2.count;
    }
    if (tableView==_tableView3) {
        return _arrayList3.count;
    }
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_tableView1) {
        OrderTable *order=_arrayList1[indexPath.section];
        CGFloat height=getTextSize(order.item_name, kMainScreen_Width-80, [UIFont LightFontOfSize:17]).height+getTextSize(order.addr_address, kMainScreen_Width-184, [UIFont LightFontOfSize:14]).height;
        return 265-39-26+height;
    }
    if (tableView==_tableView2) {
        OrderTable *order=_arrayList2[indexPath.section];
        CGFloat height=getTextSize(order.item_name, kMainScreen_Width-80, [UIFont LightFontOfSize:17]).height+getTextSize(order.addr_address, kMainScreen_Width-184, [UIFont LightFontOfSize:14]).height;
        return 265-39-26+height;
    }
    if (tableView==_tableView3) {
        OrderTable *order=_arrayList3[indexPath.section];
        CGFloat height=getTextSize(order.item_name, kMainScreen_Width-80, [UIFont LightFontOfSize:17]).height+getTextSize(order.addr_address, kMainScreen_Width-184, [UIFont LightFontOfSize:14]).height;
        return 265-39-26+height;
    }
    return CGFLOAT_MIN;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (tableView==_tableView1) {
        YCOrderDealTableViewCell *_cell = (YCOrderDealTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCOrderDealCellIdentify forIndexPath:indexPath];
        _cell.isHistory=YES;
        [_cell bindWith:_arrayList1[indexPath.section]];
        cell=_cell;
    }
    if (tableView==_tableView2) {
        YCOrderDealTableViewCell *_cell = (YCOrderDealTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCOrderDealCellIdentify forIndexPath:indexPath];
        _cell.isHistory=YES;
        [_cell bindWith:_arrayList2[indexPath.section]];
        cell=_cell;
    }
    if (tableView==_tableView3) {
        YCOrderDealTableViewCell *_cell = (YCOrderDealTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCOrderDealCellIdentify forIndexPath:indexPath];
        _cell.isHistory=YES;
        [_cell bindWith:_arrayList3[indexPath.section]];
        cell=_cell;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YCOrderDetailViewController *detailVC=[[YCOrderDetailViewController alloc]init];
    OrderTable *order=[[OrderTable alloc]init];
    if (tableView==_tableView1) {
        order=_arrayList1[indexPath.section];
    }
    if (tableView==_tableView2) {
        order=_arrayList2[indexPath.section];
    }
    if (tableView==_tableView3) {
        order=_arrayList3[indexPath.section];
    }
    detailVC.orderId=order.order_no;
    [self showNavigationView:detailVC];
}
-(void)ScrollBar:(ScrollBar *)view andBtutton:(SVButton *)button andIndex:(NSInteger)selectIndex
{
    _currentIndex =selectIndex ;
    [_contentscrollview  setContentOffset:CGPointMake(W(_contentscrollview)*_currentIndex, 0) animated:NO];
    _currentPage=1;
    [self loadData];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView==_contentscrollview) {
        NSInteger index = fabs(_contentscrollview.contentOffset.x) / _contentscrollview.frame.size.width;
        _currentIndex=index;
        [_contentscrollview  setContentOffset:CGPointMake(W(_contentscrollview)*_currentIndex, 0) animated:NO];
        _scrollBar.currentSelect = _currentIndex;
        _currentPage=1;
    }
}
-(void)btnSearchClicked:(UIButton *)button
{
    YCOrderSearchViewController *searchVC=[[YCOrderSearchViewController alloc]init];
    [self showNavigationView:searchVC];
}

@end
